/**
 * weapp-js v2.2.3(2022-4-26)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2022 Motigo, Inc.
 * 
 */
import Callback from './utils/callback';

class Ad extends Callback {
    constructor() {
        super();
        this.rewardVideoAdContext = null; //激励视频广告单元
        this.interstitialAdContext = null; //插屏广告单元
        this.rewardVideoAdId = ''; //激励视频广告单元id
        this.interstitialAdId = ''; //插屏广告单元id
        this.adContext = null; //当前广告单元
        this.type = '';
        this.config = {};
        this.initError = false;
    }

    /**
     *外部调用
     *
     * @param {String} type  广告类型 reward interstitial
     * @param {Object} config 广告配置(id必传)
     * @returns {Promise}
     */
    // handle(type, opts) {
    //     this.init(type, opts);
    //     return this.show();
    // }
    /**
     * 广告单元初始化
     *
     * @param {String} type  广告类型 reward interstitial
     * @param {Object} config 广告配置(id必传)
     */
    init({ type, config } = opts) {
        this.type = type;
        this.config = config;
        if (type === 'reward') {
            this._rewardAdInit();
        }
        if (type === 'interstitial') {
            this._interstitialAdInit();
        }
        console.log(this.adContext);
        return this;
    }
    _rewardAdInit() {
        if (this.config.id === this.rewardVideoAdId && !this.initError) {
            // 已经创建好了相同的激励广告单元
            this.adContext = this.rewardVideoAdContext;
        } else {
            this.rewardVideoAdContext = wx.createRewardedVideoAd({
                adUnitId: this.config.id,
            });

            this.adContext = this.rewardVideoAdContext;
        }
    }
    _interstitialAdInit() {
        if (this.config.id === this.interstitialAdId && !this.initError) {
            // 已经创建好了相同的插屏广告单元
            this.adContext = this.interstitialAdContext;
        } else {
            this.interstitialAdContext = wx.createInterstitialAd({
                adUnitId: this.config.id,
            });
            this.adContext = this.interstitialAdContext;
        }
    }
    /**
     * 广告单元进行播放
     *
     *
     */
    show(opts) {
        console.log('show');
        return new Promise((resolve, reject) => {
            this.adContext.onError(err => {
                // 如果广告单元加载失败，则默认为成功
                console.log(err)
                resolve(true);
            });
            this.adContext.offClose();
            this.adContext.onClose(res => {
                if (this.type === 'reward') {
                    if (res.isEnded) {
                        // 看完了广告
                        // resolve(true);
                        this.success(opts, { isEnd: true });
                    } else {
                        // 没看完
                        // resolve(false);
                        this.success(opts, { isEnd: false });
                    }
                } else {
                    // resolve(true);
                    this.success(opts, { isEnd: true });
                }
            });

            try {
                this.adContext
                    .load()
                    .then(() => {
                        this.initError = false;
                        this.adContext.show().catch(err => {
                            // resolve(true);
                            console.log(err)
                            this.success(opts, { isEnd: true, isError: true });
                        });
                    })
                    .catch(err => {
                        // resolve(true);
                        
                        this.success(opts, { isEnd: true, isError: true });
                    });
            } catch (error) {
                // 广告加载出错
                this.initError = true;
                // resolve(true);
                this.success(opts, { isEnd: true, isError: true });
            }
        });
    }
}

class AdController extends Callback {
    constructor() {
        super();
    }
    init({ type, config } = opts) {
        let ad = new Ad();
        ad.init({ type, config });
        return ad;
    }
}

export default new AdController();
